<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\article\Article as Model;
use app\util\Util;
use app\models\article\ArticleFav as Fav;
use app\models\article\ArticleCategory as Category;

class ArticleController extends H5Controller
{
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $category_id = intval($this->post('category', 0));

        $query = Model::find()
            ->where(['status' => Model::$statusMapping['OK']]);

        if ($category_id) {
            $query->andWhere(['category_id' => $category_id]);
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['image'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    /**
     * view
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionView()
    {
        $id = $this->post('id');

        $model = Model::find()
            ->where(['id' => $id])
            ->andWhere(['status' => Model::$statusMapping['OK']])
            ->with(['image'])
            ->asArray()
            ->one();
        if ($model) {
            // convert images
            $convert = Util::convertImages($model['content']);
            $model['inImages'] = $convert['images'];
            $model['inText'] = $convert['text'];

            unset($model['content']);

            return $this->successEncode($model);
        }

        return $this->nothing();
    }

    public function actionAdd()
    {
        $id = $this->post('id');
        $model = Model::findOne(['id' => $id]);
        if ($model) {
            $model->view += 1;
            $model->save();
        }

        return $this->success();
    }

    public function actionCheck()
    {
        $id = intval($this->post('id'));
        $user_id = $this->user->id;

        $model = Fav::findOne(['user_id' => $user_id, 'article_id' => $id]);
        if ($model) {
            return $this->successEncode(['is' => true]);
        }

        return $this->successEncode(['is' => false]);
    }

    public function actionCreate()
    {
        $id = intval($this->post('id'));
        $user_id = $this->user->id;

        $model = Fav::findOne(['user_id' => $user_id, 'article_id' => $id]);
        if ($model) {
            return $this->success();
        }

        $data = [
            'ArticleFav' => [
                'user_id' => $user_id,
                'article_id' => $id
            ]
        ];
        $model = new Fav();
        if ($model->load($data) && $model->save()) {
            return $this->success();
        }

        return $this->fail('收藏失败');
    }

    public function actionDelete()
    {
        $id = intval($this->post('id'));
        $user_id = $this->user->id;

        $model = Fav::findOne(['user_id' => $user_id, 'article_id' => $id]);
        if ($model && $model->delete()) {
            return $this->success();
        }

        return $this->fail('删除失败');
    }

    public function actionFavList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Fav::find()
            ->where(['user_id' => $this->user->id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['article', 'article.mainImage'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionCategory()
    {
        $list = Category::find()
            ->where(['!=', 'status', Model::$statusMapping['DELETE']])
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }
}